package com.hhs.storage.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.hhs.storage.bean.JxcMenu;
import com.hhs.storage.bean.JxcMenuRole;
import com.hhs.storage.bean.Result;
import com.hhs.storage.dao.JxcMenuDao;
import com.hhs.storage.service.JxcMenuRoleService;
import com.hhs.storage.service.JxcMenuService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shadow
 * @since 2018-09-29
 */
@Service
@Slf4j
public class JxcMenuServiceImpl extends ServiceImpl<JxcMenuDao, JxcMenu> implements JxcMenuService {

    @Resource
    JxcMenuRoleService jxcMenuRoleService;

    public List<JxcMenu> findByUserId(String userId){
        return this.baseMapper.findByUserId(userId);
    }

    @Override
    public Map<String, Object> dele( String id) {
        int count = selectCount(new EntityWrapper<JxcMenu>().eq("pid_", id));
        if(count>0){
            return Result.failJsonResp("该菜单有下级菜单，无法删除");
        }
        //先删除关联表
        jxcMenuRoleService.delete(new EntityWrapper<JxcMenuRole>().eq("menu_id_", id));
        //再删除菜单数据
        delete(new EntityWrapper<JxcMenu>().eq("id_", id));
        return Result.sucJsonResp(null);

    }

    @Override
    public List<JxcMenu> findByRoleId(String roleId) {
        return this.baseMapper.findByRoleId(roleId);
    }
}
